package com.haredot.oss.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.haredot.oss.LocalOssClient;
import com.haredot.oss.properties.OssProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

@ConditionalOnClass(OSS.class)
@EnableConfigurationProperties(OssProperties.class)
@ConditionalOnProperty(prefix = "aliyun.oss", name = "endpoint")
public class AliyunOssAutoConfiguration {

    @Bean
    @ConditionalOnProperty(prefix = "aliyun.oss", name = "enable", matchIfMissing = true)
    public OSS createOss(OssProperties ossProperties) throws Exception {
        return new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getSecretAccessKey());
    }

    @Bean
    @ConditionalOnMissingBean(OSS.class)
    public OSS createLocalOSS(OssProperties ossProperties) {
        return new LocalOssClient(ossProperties.getEndpoint());
    }
}
